package com.maixin.code.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.maixin.code.entity.UserLogin;
import com.maixin.code.utils.PageUtils;

/**
 * A data access object (DAO) providing persistence and search support for
 * UserLogin entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.maixin.UserLogin.UserLogin
 * @author MyEclipse Persistence Tools
 */

public class UserLoginDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory.getLogger(UserLoginDAO.class);

	// property constants

	public void save(UserLogin transientInstance) {
		log.debug("saving UserLogin instance");
		try {
			getSession().saveOrUpdate(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(UserLogin persistentInstance) {
		log.debug("deleting UserLogin instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public UserLogin findById(UserLogin id) {
		log.debug("getting UserLogin instance with id: " + id);
		try {
			UserLogin instance = (UserLogin) getSession().get(
					"com.maixin.pojo.UserLogin", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(UserLogin instance) {
		log.debug("finding UserLogin instance by example");
		try {
			List results = getSession()
					.createCriteria("com.maixin.pojo.UserLogin")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}
	/**
	 * 
	 * @param propertyNames
	 * @param values
	 * @return
	 */
	public List<UserLogin> findByProperties(String[] propertyNames, Object[] values){
		try {
			String queryString = "from UserLogin as model where ";
			for (int i = 0; i < propertyNames.length; i++) {
				queryString+= " model." +propertyNames[i]+ " = ?";
				if(i<propertyNames.length-1){
					queryString+= " and ";
				}
			}
					//+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			for (int i = 0; i < values.length; i++) {
				queryObject.setParameter(i, values[i]);
			}
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding UserLogin instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from UserLogin as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	public List<UserLogin> findAll() {
		log.debug("finding all UserLogin instances");
		try {
			String queryString = "from UserLogin";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	public List<UserLogin> findAllByPage(PageUtils pageUtils) {
		log.debug("finding all UserLogin instances");
		try {
			String queryString = "from UserLogin";
			Query queryObject = getSession().createQuery(queryString).
					setFirstResult((pageUtils.getNextPage() - 1) * PageUtils.pageSize).
				      setMaxResults(pageUtils.getNextPage() * PageUtils.pageSize);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	
}